/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine2D.graphics.animation;


import java.util.ArrayList;

/**
 *
 * @author Garys
 */
public class Animation {

    private ArrayList<Frame> frames;
    private int updateInterval;
    private int currentFrame;
    private boolean isLoop;
    private long lastTime;
    private long currentTime;
    private boolean running;

    /**
     * 
     */
    public Animation() {
        init();
    }
    private void init() {
        frames = new ArrayList<Frame>() {};
        updateInterval = 100; 
        currentFrame = 0;
        isLoop = true; 
        running = true;
        lastTime = System.currentTimeMillis();
        currentTime = lastTime;
        
    }
    
    /**
     * 
     * @param ticks
     */
    public void tick(int ticks) {
        
        currentTime = System.currentTimeMillis();
        if (currentTime - lastTime > updateInterval && running) {
            currentFrame++;
            lastTime = currentTime;

            if (currentFrame >= frames.size()) {
                currentFrame = 0;
                if (!isLoop)
                    running = false;
            }
        }
    
    }
    
    /**
     * 
     * @param x
     * @param y
     */
    public void render(int x, int y) {
        
       frames.get(currentFrame).render(x, y);
    
    }
    
    /**
     * 
     */
    public void reset() {
        currentFrame = 0;
        running = true;
    }
    
    /**
     * 
     * @param newFrame
     */
    public void addFrame(Frame newFrame) {
        frames.add(newFrame);
    }
    
    /**
     * 
     * @param isLoop
     */
    public void setAnimationLoop(boolean isLoop) {
        this.isLoop = isLoop;
    }
    
    /**
     * 
     * @param updateInterval
     */
    public void setAnimationUpdateInterval(int updateInterval) {
        this.updateInterval = updateInterval;
    }
    
    /**
     * 
     * @return
     */
    public boolean getAnimationRunning() {
        return running;
    }

    public void stop() {
        running = false;
    }
}
